package com.kehutong.crm.message.service;

import java.util.List;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.util.Enums;
import org.coraframework.util.Objects;
import com.kehutong.common.enums.MessageBusinessEnum;
import com.kehutong.common.enums.TemplateType;
import com.kehutong.crm.message.entity.MessageGlobalConfig;
import com.kehutong.crm.message.entity.MessageTemplateApp;
import com.kehutong.crm.message.entity.MessageTemplateInner;
import com.kehutong.crm.message.entity.MessageTemplateSms;

@WebService("/crm/message/global/config/service")
public class MessageGlobalConfigService {

    @Inject
    private JdbcSession jdbcSession;

    public Object list(JSONObject jsonObject){
        return Result.success(jdbcSession.findPage(MessageGlobalConfig.class)
                                         .eq("deleted", false)
                                         .order("createTIme", "desc")
                                         .page(jsonObject)
                                         .exe());
    }

    public Object updateSend(JSONObject jsonObject){
        String id = jsonObject.getString("id");
        if (Objects.isEmpty(id))
            return Result.error(0, "id是必传参数");

        MessageGlobalConfig config = Pool.get(MessageGlobalConfig.class, id);
        if (Objects.isNull(config))
            return Result.error(0, "不存在该配置，请检查参数后重试");

        Boolean appSend = jsonObject.getBoolean("appSend");

        if (Objects.nonNull(appSend))
            config.setAppSend(appSend);
        Boolean innerSend = jsonObject.getBoolean("innerSend");
        if (Objects.nonNull(innerSend))
            config.setInnerSend(innerSend);

        Boolean smsSend = jsonObject.getBoolean("smsSend");
        if (Objects.nonNull(smsSend))
            config.setSmsSend(smsSend);

        Boolean wxchatSend = jsonObject.getBoolean("wxchatSend");
        if (Objects.nonNull(wxchatSend))
            config.setWxchatSend(wxchatSend);

        jdbcSession.updateById(config);
        return Result.success();
    }

    public synchronized void save(MessageGlobalConfig globalConfig){
        jdbcSession.insert(globalConfig);
    }

    public synchronized void update(MessageGlobalConfig globalConfig){
        jdbcSession.updateById(globalConfig);
    }

    @ReqMapping("/sync")
    public void syncGlobalConfig(String businessType, String templateType, String wxchatTemplate){
        MessageGlobalConfig config = jdbcSession.findOne(MessageGlobalConfig.class)
                                                .eq("templateEnum", templateType)
                                                .eq("deleted", false)
                                                .exe();

        if (Objects.isNull(config)) {
            MessageBusinessEnum businessEnum = Enums.getEnum(MessageBusinessEnum.class, businessType);
            TemplateType templateEnum = Enums.getEnum(TemplateType.class, templateType);
            if (Objects.isNull(businessEnum) || Objects.isNull(templateEnum) || Objects.isEmpty(wxchatTemplate))
                return;

            config = Pool.newInstance(MessageGlobalConfig.class);
            config.setBusinessEnum(businessEnum);
            config.setTemplateEnum(templateEnum);
            config.setWxchatTemplate(wxchatTemplate);

            jdbcSession.insert(config);
        } else {
            config.setWxchatTemplate(wxchatTemplate);

            jdbcSession.updateById(config);
        }
    }

    public MessageGlobalConfig getGlobalConfig(String templateType){
        return jdbcSession.findOne(MessageGlobalConfig.class)
                          .eq("templateEnum", templateType)
                          .eq("deleted", false)
                          .exe();
    }

    @ReqMapping("/get")
    public Object getConfig(String templateType){
        MessageGlobalConfig config = jdbcSession.findOne(MessageGlobalConfig.class)
                                                .eq("templateEnum", templateType)
                                                .eq("deleted", false)
                                                .exe();
        if (Objects.isNull(config)){
            return Result.error(0, "不存在该配置");
        }

        if (Objects.nonEmpty(config.getAppTemplate())) {
            config.setMessageTemplateApp(Pool.get(MessageTemplateApp.class, config.getAppTemplate()));
        }

        if (Objects.nonEmpty(config.getInnerTemplate())) {
            config.setMessageTemplateInner(Pool.get(MessageTemplateInner.class, config.getInnerTemplate()));
        }

        if (Objects.nonEmpty(config.getSmsTemplate())) {
            config.setMessageTemplateSms(Pool.get(MessageTemplateSms.class, config.getSmsTemplate()));
        }

        return Result.success(config);
    }

    public List<MessageGlobalConfig> listGlobalConfig(JSONObject jsonObject){
        return jdbcSession.findArray(MessageGlobalConfig.class)
                          .eq(jsonObject, "appTemplate", "innerTemplate", "smsTemplate", "wxchatTemplate")
                          .eq("deleted", false)
                          .exe();
    }

}
